 
  
  var provideCode="";
  var cityCode="";
  var townCode="";
  $(function(){
	  var result=parent.dataDetail;
 		loadBaseInfo(result);
 	    getProvince();
 	    getDealer();
 
 });
  function getDealer(){
		 $.ajax({
			 url: serverUrl+'/dms-api/dealer/getDealer',
		        type: 'post',//提交的方式
		        dataType:'json',
		        async:false,
		        success: function(obj) {
		        	if( obj.status==1){
		        		 var result = obj.data;
		        		 $("#dealerId").append('<option value="">请选择经销商</option>');
		        		 for(var i=0;i<result.length;i++){
		 				  $("#dealerId").append('<option value='+result[i].id+'>'+ result[i].dealer_name+'</option>');
		                  }
		        		 renderForm();
		        	} else {
		       		 layer.msg(obj.msg);
		        	}  
		        },
		        error:function(result) {
					Layer.showFailMsg("获取经销商失败！稍后再试！");
		        }
			})
	}
function loadBaseInfo(result){
	$("input[name='actualpersonSex'][value= '男']").attr("checked",true);	 
	 var storeId=result.storeId;
   	 
	 $.ajax({
	        url:  hostUrl + "/dealer/searchStoreByStoreId",
	        type: 'post',//提交的方式
	        dataType:'json',
	        async:false,
	        data: {"storeId":storeId},
	        success: function(obj) {
	        	if( obj.status==1){
	        		var data = obj.data;
  	        		/*if(result.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    legalpersonSex=='男'){
 	                   $("input[name='legalpersonSex'][value='男']").attr("checked",true);  
	        		}else{
	        			
	        			$("input[name='legalpersonSex'][value='女']").attr("checked",true);
	        		}
 	        		if(result.actualpersonSex=='男'){
 	        		   $("input[name='actualpersonSex'][value= '男']").attr("checked",true);    
 	        		}else{
 	        			
 	        			$("input[name='actualpersonSex'][value= '女']").attr("checked",true);    
 	        		}*/
 	        		for(var i=0;i<data.length;i++){
	        			if(data[i].open_business=='新车销售'){
	        				 $("input[name='start_service'][value='1']").attr("checked",true); 
	        			}
	        			if(data[i].open_business=='零售金融'){
	        				 $("input[name='start_service'][value='2']").attr("checked",true); 
	        			}
	        			if(data[i].open_business=='保险延保'){
	        				 $("input[name='start_service'][value='3']").attr("checked",true); 
	        			}
	        			if(data[i].open_business=='展融额度'){
	        				 $("input[name='start_service'][value='4']").attr("checked",true); 
	        			}
	        			if(data[i].open_business=='洗美业务'){
	        				 $("input[name='start_service'][value='5']").attr("checked",true); 
	        			}
	        		}
	               	$("#storeName").val(data[0].store_name);
	        		$("#storeCode").val(data[0].store_code);  
	        		$("#id").val(storeId);
  	        		$("#dealerId").append('<option value='+data[0].dealer_id+'>'+ result.dealer_name+'</option>');
	        		$("#storeAddress").val(data[0].store_address);
	        		$("#longitude").val(data[0].longitude);
	        		$("#latitude").val(data[0].latitude);
	        	    $("#businessTime").val(data[0].business_time);
	        		 
	          		 cityCode=data[0].city_code; 
 	          		 townCode=data[0].town_code;
	          		 provideCode=data[0].province_code; 
  					 
	        	} else {
	       		 layer.msg(obj.msg);
	       	}  
	        },
	        error:function(result) {
				Layer.showFailMsg("获取省市信息失败！稍后再试！");
	        }
		})
 
}
	
	 
  
 
//重新渲染表单
 function renderForm(){
   layui.use('form', function(){
    var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
    	  form.render();
   });
  }
 layui.use(['layer','form','element'], function(){
	    var layer = layui.layer,
	        $ = layui.jquery;
	    var form = layui.form;
	    form.on('select(selectCity)', function(data){
	        selectCity();
	    });
	    form.on('select(selectTown)', function(data){
	    	selectTown();
	    });
	    
	    //监听提交
        form.on('submit(storeEditSave)', function(data){
       	   var sex = $("input[name='start_service']:checked").val();
       	 if(typeof(sex) == 'undefined' || sex == ''){
    		 Layer.showFailMsg("【开通业务】为必填字段");
    		 return false;
    	 }  
       	 
       	 var checkBoxVal = '';
        for(var i=0;i<$("input[name=start_service]:checked").length;i++){
       		checkBoxVal+=$("input[name=start_service]:checked").eq(i).val()+','
       	} 
        $("#busType").val(checkBoxVal);
       	 $.ajax({
    			cache: true,
    			type: "POST",
    			url:  "/dms-web/dealer/storeEditSave",
    			data: $('#store').serialize(),// 你的formid,获取所有值
    			async: false,
    			 
    			success: function(data) { 
    				if(data.status==0){
    					 Layer.showSucMsg("修改成功");
    					 parent.layer.closeAll();
    					 parent.location.reload();
    				}else{
    					Layer.showFailMsg("修改失败");
    				}
    			 
    					 
    			},
    			error: function(data) { 
    		 		Layer.showFailMsg("操作失败");
    		 	}
    		});   
          return false;
        });
	});
 //获取省
 function getProvince(){//   url: 'http://127.0.0.1:8080/dms-api/address/getAddress',
	 var pr =provideCode;
//	 var prss = $("#provinceList").val();
// 	  var pr = ${data.province_code};
	 	$.ajax({
	         url: serverUrl+'/dms-api/address/getAddress',
	         type: 'post',//提交的方式
	         dataType:'json',
	         success: function(data) {
	             //这是成功返回的数据，写自己的逻辑
	 			if(data.status==1){
	 				$("#provinceList").append('<option value="">请选择省</option>');
	 				$("#cityList option").remove();
	 				for(var i=0;i<data.data.length;i++){
	 					if(pr == data.data[i].code){
	 						$("#provinceList").append('<option selected value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
	 						selectCity();
	 					}else{
	 					$("#provinceList").append('<option value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
	 					}
	 				}
	 				renderForm();
	 			}else{
	 				Layer.showFailMsg("获取省市信息失败！稍后再试！");
	 			}
	         },
	         error:function(result) {
	         	Layer.showFailMsg("获取省市信息失败！稍后再试！");
	         }
	 	})
	 }
 function selectCity(){
	 var pr = "";
	 var prss = $("#provinceList").val();
	 if(prss == null){
		 pr = provideCode;
	 }else{
		 var prArr = prss.split(";");
		 pr = prArr[0];
	 }
		$.ajax({
	        url: serverUrl+'/dms-api/address/getAddress',
	        type: 'post',//提交的方式
	        dataType:'json',
	        data: {"provinceCode":pr},
	        success: function(data) {
	            //这是成功返回的数据，写自己的逻辑
				if(data.status==1){
					$("#cityList option").remove();
					$("#townList option").remove();
					$("#cityList").append('<option value="">请选择市</option>');
					for(var i=0;i<data.data.length;i++){
 				
						if(cityCode == data.data[i].code){
							$("#cityList").append('<option selected value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
							selectTown();
						}else{
							$("#cityList").append('<option value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
						}
					}
					renderForm();
					$("#townList").html("");
				}else{
					Layer.showFailMsg("获取省市信息失败！稍后再试！");
				}
	        },
	        error:function(result) {
	        	Layer.showFailMsg("获取省市信息失败！稍后再试！");
	        }
		})
	}

	function selectTown(){
		 var ci = "";
		 var citycode = $("#cityList").val();
		 if(citycode == null){
			 ci = cityCode;
		 }else{
			 var ciArr = citycode.split(";");
			 ci = ciArr[0];
		 }
		$.ajax({
	        url: serverUrl+'/dms-api/address/getAddress',
	        type: 'post',//提交的方式
	        dataType:'json',
	        data: {"cityCode":ci},
	        success: function(data) {
	            //这是成功返回的数据，写自己的逻辑
				if(data.status==1){
					$("#townList option").remove();
					for(var i=0;i<data.data.length;i++){
						if(townCode == data.data[i].code){
							$("#townList").append('<option selected value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
						}else{
							$("#townList").append('<option value='+data.data[i].code+';'+data.data[i].name+'>'+data.data[i].name+'</option>');
						}
					}
					renderForm();
				}else{
					Layer.showFailMsg("获取省市信息失败！稍后再试！");
				}
	        },
	        error:function(result) {
				Layer.showFailMsg("获取省市信息失败！稍后再试！");
	        }
		})
	}
 
 renderForm();
   
   